/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.camera;

import com.jme3.collision.CollisionResults;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.input.InputManager;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.navid.trafalgar.model.AShipModel;
import java.io.IOException;

/**
 *
 * @author alberto
 */
public class TargetCamera implements Control {

    Camera cam;
    AShipModel reference;
    Node target;
    private boolean enabled;

    public TargetCamera(Camera cam, AShipModel reference) {
        this.cam = cam;
        reference.addControl(this);
        this.reference = reference;
    }

    public TargetCamera(Camera cam, AShipModel reference, Node target) {
        this.cam = cam;
        reference.addControl(this);
        this.reference = reference;
        this.target = target;
    }

    public void setTarget(Node target) {
        this.target = target;
    }

    public void update(float tpf) {

        if (enabled) {
            Vector3f referencePosition = reference.getWorldTranslation();
            Vector3f difference = target.getWorldTranslation().subtract(reference.getWorldTranslation());

            Vector3f normal = difference.normalize().mult(40);
            normal.y = -15;

            cam.setLocation(referencePosition.subtract(normal));

            cam.lookAt(target.getWorldTranslation(), Vector3f.ZERO);

          /*  Ray ray = new Ray(cam.getLocation(), target.getWorldScale().subtract(cam.getLocation()));
            CollisionResults results = new CollisionResults();

            if (reference.collideWith(ray, results) == 0) {

                reference.setTransparent(true);

            } else {
                reference.setTransparent(false);
            }*/
        }

    }

    public void render(RenderManager rm, ViewPort vp) {
    }

    public void setSpatial(Spatial spatial) {
    }

    public void write(JmeExporter ex) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void read(JmeImporter im) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the enabled
     */
    public boolean isEnabled() {
        return enabled;
        
    }

    /**
     * @param enabled the enabled to set
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        
            reference.setTransparent(enabled);
        
    }

    public void registerWithInput(InputManager inputManager) {
    }
}
